package com.sanyang.pda.ui.manualdelivery;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sanyang.pda.adapter.PutOnAdapter;
import com.sanyang.pda.bean.PalletBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.request.PutOnRequest;
import com.sanyang.pda.contract.PutOnContract;
import com.sanyang.pda.presenter.PutOnPresenter;
import com.sanyang.pda.view.ScanButView;
import com.sanyang.pda.view.datepicker.CustomDatePicker;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class PutOnFragment extends BaseMvpFragment<PutOnPresenter> implements PutOnContract.View {


    @BindView(R.id.tv_now_location)
    TextView tvNowLocation;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.bt_putaway)
    Button btPutaway;
    @BindView(R.id.scan)
    ScanButView scan;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.bt_quliao_putaway)
    Button btQuliaoPutaway;
    private CustomDatePicker mDatePicker;
    private List<PalletBean> palletBeans;
    PutOnAdapter adapter;
    private int carryUp;
    public PutOnFragment(String entryNo) {
        this.entryNo = entryNo;
    }

    private String entryNo;

    @Override
    public int getViewId() {
        return R.layout.fragment_put_on;
    }

    @Override
    public void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter=new PutOnAdapter(R.layout.item_put_on,new ArrayList<>());
        adapter.setEntryNo(entryNo,this);
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {

                switch (scanBean.getType()) {
                    case 1://作业库位
                        if (TextUtils.isEmpty(tvTray.getText())){
                            showMessage(getString(R.string.tray)+getString(R.string.not_be_empty));
                            return;
                        }
                        if (carryUp == 10){
                            return;
                        }
                        tvNowLocation.setText(scanBean.getCode());
                        break;
                    case 2://作业库位
                        if (TextUtils.isEmpty(tvTray.getText())){
                            showMessage(getString(R.string.tray)+getString(R.string.not_be_empty));
                            return;
                        }
                        if (carryUp == 20){
                            return;
                        }
                        tvNowLocation.setText(scanBean.getCode());
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        presenter.palletizingInfo(scanBean.getCode(), entryNo);
                        break;

                }
            }
        });
    }

    @Override
    public void getData() {

    }



    @Override
    public PutOnPresenter getPresenter() {
        return new PutOnPresenter();
    }
    @Override
    public void setPallet(List<PalletBean> pallet) {
        if (pallet.size() > 0){
            carryUp= Integer.parseInt(pallet.get(0).getCarryUp());
            if (pallet.get(0).getCarryUp().equals("10")){
                btPutaway.setVisibility(View.INVISIBLE);
                btQuliaoPutaway.setVisibility(View.VISIBLE);
            }
            if (pallet.get(0).getCarryUp().equals("30")){
                btQuliaoPutaway.setVisibility(View.INVISIBLE);
                btPutaway.setVisibility(View.VISIBLE);
            }
        }
        adapter.setNewInstance(pallet);
    }

    @Override
    public void putSuccess() {
        tvTray.setText("");
        tvNowLocation.setText("");
        adapter.setNewInstance(new ArrayList<>());
        btQuliaoPutaway.setVisibility(View.INVISIBLE);
        btPutaway.setVisibility(View.INVISIBLE);
    }

    @Override
    public void reclaimingSuccess() {
        presenter.palletizingInfo(tvTray.getText().toString(), entryNo);
    }

    @OnClick(R.id.bt_putaway)
    public void onViewClicked() {
        if (TextUtils.isEmpty(tvTray.getText())) {
            showMessage(getString(R.string.no_tray));
            return;
        }
        if (TextUtils.isEmpty(tvNowLocation.getText())) {
            showMessage(getString(R.string.no_nowlocation));
            return;
        }
        PutOnRequest putOnRequest = new PutOnRequest();
        putOnRequest.setEntryNo(entryNo);
        putOnRequest.setStoragebinId(tvNowLocation.getText().toString());
        putOnRequest.setPallet(tvTray.getText().toString());
        putOnRequest.setArticleInfoList(adapter.getData());
        presenter.putOnShelves(putOnRequest);
    }

    @OnClick(R.id.bt_quliao_putaway)
    public void onViewClicked1() {
        if (TextUtils.isEmpty(tvTray.getText())) {
            showMessage(getString(R.string.no_tray));
            return;
        }
        if (TextUtils.isEmpty(tvNowLocation.getText())) {
            showMessage(getString(R.string.no_nowlocation));
            return;
        }
        PutOnRequest putOnRequest = new PutOnRequest();
        putOnRequest.setEntryNo(entryNo);
        putOnRequest.setCurrentStoragebinId(tvNowLocation.getText().toString());
        putOnRequest.setPallet(tvTray.getText().toString());
        putOnRequest.setArticleInfoList(adapter.getData());
        presenter.reclaimingPutOnShelves(putOnRequest);
    }
}
